package com.yuexunit.finance.system.service;

import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;

import com.yuexunit.finance.system.entity.LendUnitInfoEntity;
import com.yuexunit.inject.annotation.Implements;
import com.yuexunit.page.PageCriteria;
import com.yuexunit.page.PageList;
import com.yuexunit.persist.PersistUtil;
import com.yuexunit.persist.annotation.Transactional;
import com.yuexunit.util.DateUtil;
import com.yuexunit.util.StringUtil;

/**
 * @author wenwenlong
 */
@Implements(LendUnitInfoService.class)
public class LendUnitInfoServiceImpl implements LendUnitInfoService {

	@Override
	@Transactional
	public Long createLendUnitInfo(LendUnitInfoEntity entity) {
		entity.setCreateDateTime(DateUtil.now());
		entity.setUpdateDateTime(DateUtil.now());
		PersistUtil.insert(entity);
		return entity.getLendUnitId();
	}

	@Override
	@Transactional
	public void updateLendUnitInfo(LendUnitInfoEntity entity) {
		entity.setUpdateDateTime(DateUtil.now());
		PersistUtil.update(entity);
	}

	@Override
	public PageList<LendUnitInfoEntity> inquireLendUnitInfoPagelist(String lendUnitName, String lendUnitBank, String lendUnitBankCard, String lendUnitBnakName,
			PageCriteria page) {
		DetachedCriteria criteria = DetachedCriteria.forClass(LendUnitInfoEntity.class);
		if (StringUtil.isNotBlank(lendUnitName)) {
			criteria.add(Restrictions.like("lendUnitName", "%" + lendUnitName + "%"));
		}
		if (StringUtil.isNotBlank(lendUnitBank)) {
			criteria.add(Restrictions.like("lendUnitBank", "%" + lendUnitBank + "%"));
		}
		if (StringUtil.isNotBlank(lendUnitBankCard)) {
			criteria.add(Restrictions.like("lendUnitBankCard", "%" + lendUnitBankCard + "%"));
		}
		if (StringUtil.isNotBlank(lendUnitBnakName)) {
			criteria.add(Restrictions.like("lendUnitBnakName", "%" + lendUnitBnakName + "%"));
		}
		return PersistUtil.get(criteria, page);
	}
	
	@Override
	public List<LendUnitInfoEntity> inquireLendUnitInfoList(String lendUnitName, String lendUnitBank, String lendUnitBankCard, String lendUnitBnakName) {
		DetachedCriteria criteria = DetachedCriteria.forClass(LendUnitInfoEntity.class).add(Restrictions.eq("enabled", Boolean.TRUE));
		if (StringUtil.isNotBlank(lendUnitName)) {
			criteria.add(Restrictions.like("lendUnitName", "%" + lendUnitName + "%"));
		}
		if (StringUtil.isNotBlank(lendUnitBank)) {
			criteria.add(Restrictions.like("lendUnitBank", "%" + lendUnitBank + "%"));
		}
		if (StringUtil.isNotBlank(lendUnitBankCard)) {
			criteria.add(Restrictions.like("lendUnitBankCard", "%" + lendUnitBankCard + "%"));
		}
		if (StringUtil.isNotBlank(lendUnitBnakName)) {
			criteria.add(Restrictions.like("lendUnitBnakName", "%" + lendUnitBnakName + "%"));
		}
		return PersistUtil.get(criteria);
	}
	
	@Override
	public List<LendUnitInfoEntity> inquireLendUnitInfoList(String lendUnitName) {
		DetachedCriteria criteria = DetachedCriteria.forClass(LendUnitInfoEntity.class);
		if (StringUtil.isNotBlank(lendUnitName)) {
			criteria.add(Restrictions.like("lendUnitName", "%" + lendUnitName + "%"));
		}
		return PersistUtil.get(criteria);
	}

	@Override
	public LendUnitInfoEntity inquireLendUnitInfoDetail(Long lendUnitId) {
		return PersistUtil.get(LendUnitInfoEntity.class, lendUnitId);
	}
}
